//
//  MainTabbarController.m
//  NewPro
//
//  Created by 何荧红 on 2018/10/1.
//  Copyright © 2018 何荧红. All rights reserved.
//

#import "MainTabbarController.h"
#import "LoginViewController.h"
#import "AchievementVC.h"
#import "FriendViewController.h"
#import "MyCenterVC.h"
#import "UINavigationBar+handle.h"
#import "MineStore.h"
#import "UserModel.h"
#import "BaseNavgationViewController.h"
#import "NewHomeViewController.h"

@interface MainTabbarController ()<UITabBarDelegate,UITabBarControllerDelegate>

@property (nonatomic,weak) UITabBarItem *item;

@end

@implementation MainTabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    //去除tabbar的横线
    [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
    [self.tabBar setClipsToBounds:NO];
    [self bulidTabBarController];
    [self.tabBar setBackgroundColor:[UIColor whiteColor]];
    self.tabBar.translucent = NO;
    self.delegate = self;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setRed:) name:@"SETMINERED" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"REALNAMEING" object:nil];
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)bulidTabBarController
{
    [self setupChildViewController:@"首页" viewController:[NewHomeViewController new] image:@"first_normal" selectedImage:@"first_selected"];
    
    [self setupChildViewController:@"业绩" viewController:[AchievementVC new] image:@"second_normal" selectedImage:@"second_selected"];
    
    [self setupChildViewController:@"合伙人" viewController:[FriendViewController new] image:@"third_normal" selectedImage:@"third_selected"];

    [self setupChildViewController:@"我的" viewController:[MyCenterVC new] image:@"four_normal" selectedImage:@"four_selected"];
    
}

- (void)setupChildViewController:(NSString *)title viewController:(UIViewController *)controller image:(NSString *)image selectedImage:(NSString *)selectedImage {
    
    UITabBarItem *item = [[UITabBarItem alloc]init];

    if ([title isEqualToString:@"我的"]) {
        
        self.item = item;
        
        [self refresh];
    }
    
    item.image = [[UIImage imageNamed:image]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    item.selectedImage = [[UIImage imageNamed:selectedImage]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    item.title = title;
    controller.tabBarItem = item;
    UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:controller];
    [navController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];

    [navController.navigationBar navBarBackGroundColor:BLUECOLOR image:nil isOpaque:YES];//颜色    
    navController.navigationBar.translucent = NO;
    //隐藏导航栏底部线
    [navController.navigationBar setShadowImage:[[UIImage alloc] init]];
    [navController.navigationBar setTintColor:[UIColor whiteColor]];
    //    //传图片名字
    //    navController.imageName =@"goback";
    [navController.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -2)];
    
    [self addChildViewController:navController];
    
//    [self refresh];
    
}

+ (void)load{
    // 获取当前类的tabBarItem
    UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];
    // 设置所有item的选中时颜色
    // 设置选中文字颜色
    // 创建字典去描述文本
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    // 文本颜色 -> 描述富文本属性的key -> NSAttributedString.h
    attr[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"0CA6F0"];
    attr[NSFontAttributeName] = [UIFont fontWithName:@"PingFang-SC-Medium" size:10];
    [item setTitleTextAttributes:attr forState:UIControlStateSelected];
    
    // 通过normal状态设置字体大小
    // 字体大小 跟 normal
    NSMutableDictionary *attrnor = [NSMutableDictionary dictionary];
    
    // 设置字体
    attrnor[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"999999"];
    attrnor[NSFontAttributeName] = [UIFont fontWithName:@"PingFang-SC-Medium" size:10];
    
    [item setTitleTextAttributes:attrnor forState:UIControlStateNormal];
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    return YES;
}


- (void)refresh{
    
    MineStore *store = [[MineStore alloc] init];
    
    [store getUserInfoWithCustomId:[UserDefaultsManager sharedInstance].user_id Success:^(UserModel *model) {
    
        NSString *str = [NSString stringWithFormat:@"%@",model.readnum];
        
        if (![str isEqualToString:@"0"] && ![str isEqualToString:@"(null)"]) {
            
            
            self.item.badgeValue = str;
        }else{
            
            self.item.badgeValue = nil;
        }
        
        
    } Failure:^(NSError *error) {
    
    }];
}

- (void)setRed:(NSNotification *)noti{
    
    NSString *str = noti.userInfo[@"info"];
    self.item.badgeValue = str;
}
@end
